package com.logos.musicframe;

import com.logos.musicframe.ui.javafx.LoginFxUi;
import com.logos.musicframe.ui.javafx.MainMusicPlayerUi;
import com.logos.musicframe.utils.*;
import javafx.application.Application;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.swing.SwingUtilities;

/**
 * @author logos
 * date 2025/5/29 13:12
 * @version v1.0
 * @Package com.logos.musicframe
 */
@Slf4j
@SpringBootApplication(scanBasePackages = "com.logos.musicframe")
@EnableScheduling
@EnableTransactionManagement
@MapperScan("com.logos.musicframe.mapper")
public class MusicFrameApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MusicFrameApplication.class);
        app.setWebApplicationType(WebApplicationType.NONE);
        app.run(args);
        String[] session = LocalSessionStorageUtil.getLoginSession();
        if (session[0] != null && session[1] != null) {
            log.info("已有本地登录态:key{},value{}", session[0], session[1]);
            // 已有本地登录态，直接进入主界面
            SwingUtilities.invokeLater(() -> {
                MainMusicPlayerUi mainMusicPlayerUi = SpringContextUtil.getBean(MainMusicPlayerUi.class);
                new Thread(() -> {
                    Application.launch(mainMusicPlayerUi.getClass());
                }).start();
            });

        } else {
            SwingUtilities.invokeLater(() -> {
                LoginFxUi loginFxUi = SpringContextUtil.getBean(LoginFxUi.class);
                new Thread(() -> {
                    Application.launch(loginFxUi.getClass());
                }).start();
            });
        }
    }

}
